package com.swt.gap.util;

import com.swt.gap.model.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.mgt.DefaultSessionKey;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import javax.servlet.http.HttpServletRequest;

/**
 * created by lanshengyuan on 2019/10/25
 */
public class UserLoginUtils {

//    public static String getUserNameBySessionId(String sessionid) {
//
//        Object obj = SecurityUtils.getSecurityManager().getSession(new DefaultSessionKey(sessionid))
//                .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
//
//        SimplePrincipalCollection coll = (SimplePrincipalCollection) obj;
//
//       return coll.getPrimaryPrincipal().toString();
//    }

    public static SysUser getUser(){
        Subject subject = SecurityUtils.getSubject();
        return (SysUser) subject.getPrincipal();
    }

    public static Integer getUserId(){
        return getUser().getUserId();
    }
//
//    public  static String getToken(HttpServletRequest request){
//        return request.getHeader("token");
//    }
//
//    public static String getUserNameByHttp(HttpServletRequest request){
//
//        return getUserNameBySessionId(getToken(request));
//
//    }
    public static String passwordEncoder(String credentials) {
        Object object = new SimpleHash("MD5", credentials, null, 2);
        return object.toString();
    }

    public  static boolean passwd(String passwd){

        int num = 0;

        if (passwd.matches(".*?[a-z]+.*?")) {
            num++;
        }
        if (passwd.matches(".*?[A-Z]+.*?")) {
            num++;
        }
        if(passwd.matches(".*?[0-9]+.*?")) {
            num++;
        }
        if(passwd.matches(".*?[^a-zA-Z\\d]+.*?")) {
            num++;
        }
        return num>=3;
    }
}
